export class Grid {
	public x: number;
	public y: number;
	public version: boolean;
	public nowCost: number;
	public mayCost: number;
	public parent: Grid;
	public next: Grid;
	public pre: Grid;
	public neighbors: Grid[];
	public count = 0;
	public dist = 0;

	constructor(x: number, y: number) {
		this.x = x;
		this.y = y;
	}

	public checkNeighbors(map: any, blocks: any) {
		this.neighbors = [];

		if (this.x > 0) {
			this.addNeighbor(this.x - 1, this.y, map, blocks);
		}

		if (this.y > 0) {
			this.addNeighbor(this.x, this.y - 1, map, blocks);
		}

		this.addNeighbor(this.x + 1, this.y, map, blocks);
		this.addNeighbor(this.x, this.y + 1, map, blocks);
	}

	private addNeighbor(x: number, y: number, map: any, blocks: any) {
		const id = getId(x, y);
		if (!blocks[id] && !map[id]) {
			map[id] = new Grid(x, y);
			this.neighbors.push(map[id]);
			this.count++;
		}
	}
}

export const getId = (x: number, y: number): string => {
	return `${y}*${x}`;
};
